﻿using Delphi.Isis.Yahoo;
using Delphi.Mimir.Config;
using Delphi.Mimir.Data.Entities.Final;
using Delphi.Mimir.Data.Entities.Raw;
using System;
using System.Collections.Generic;

namespace Delphi.Mimir.Wrapper {
    public static class AnalystEstimateWrapper {

        public static void AddEstimates(List<YahooAnalystEstimates> estimates) {
            if(estimates == null) {
                return;
            }

            DateTime nullDate = new DateTime();

            using (var context = new DelphiContext()) {
                foreach (YahooAnalystEstimates e in estimates) {
                    Symbol sym = SymbolWrapper.GetSymbol(e.YahooCode);

                    RawEarningsEstimate ee = new RawEarningsEstimate();
                    RawEarningsHistory eh = new RawEarningsHistory();
                    RawGrowthEstimate ge = new RawGrowthEstimate();
                    RawEpsRevision er = new RawEpsRevision();
                    RawEpsTrend et = new RawEpsTrend();
                    RawRevenueEstimate re = new RawRevenueEstimate();

                    ee.aee_avg_estimate = e.EarningsEstimateAverage;
                    ee.aee_dt = e.EarningsEstimateDate;
                    ee.aee_exc_cd = sym.Exchange.Code;
                    ee.aee_high_estimate = e.EarningsEstimateHigh;
                    ee.aee_low_estimate = e.EarningsEstimateLow;
                    if(e.NextEarningsDate == nullDate) {
                        ee.aee_next_earnings_dt = null;
                    } else {
                        ee.aee_next_earnings_dt = e.NextEarningsDate;
                    }
                    ee.aee_nof_analyst = e.EarningsEstimateNof;
                    ee.aee_period = e.EarningsEstimatePeriod;
                    ee.aee_sym_cd = sym.Code;
                    ee.aee_year_ago = e.EarningsEstimateYearAgo;
                    ee.aee_curt_fg = e.EarningsEstimatesCurrent;

                    re.aer_avg_estimate = e.RevenueEstimateAvg;
                    re.aer_dt = DateTime.UtcNow;
                    re.aer_estimate_dt = e.RevenueEstimateDate;
                    re.aer_exc_cd = sym.Exchange.Code;
                    re.aer_high_estimate = e.RevenueEstimateHigh;
                    re.aer_low_estimate = e.RevenueEstimateLow;
                    re.aer_nof_analyst = (int)e.RevenueEstimateNof;
                    re.aer_period = e.RevenueEstimatePeriod;
                    re.aer_sales_growth = e.RevenueEstimateSalesGrowth;
                    re.aer_sym_cd = sym.Code;
                    re.aer_year_ago = e.RevenueEstimateYearAgo;
                    re.aer_curt_fg = e.RevenueEstimateCurrent;

                    ge.age_comp_typ = e.GrowthEstimateType;
                    ge.age_curt_qtr = e.GrowthEstimateCurrentQuarter;
                    ge.age_curt_year = e.GrowthEstimateThisYear;
                    ge.age_dt = DateTime.UtcNow;
                    ge.age_exc_cd = sym.Exchange.Code;
                    ge.age_next_5y = e.GrowthEstimateNext5Years;
                    ge.age_next_qtr = e.GrowthEstimateNextQuarter;
                    ge.age_next_year = e.GrowthEstimateNextYear;
                    ge.age_past_5y = e.GrowthEstimatePast5Years;
                    ge.age_pe = e.GrowthEstimatePe;
                    ge.age_peg = e.GrowthEstimatePeg;
                    ge.age_sym_cd = sym.Code;

                    er.arv_curt_fg = e.EpsRevisionsCurrent;
                    er.arv_down_30d = e.EpsRevisionsDown30Days;
                    er.arv_down_90d = e.EpsRevisionsDown90Days;
                    er.arv_dt = e.EpsRevisionDate;
                    er.arv_exc_cd = sym.Exchange.Code;
                    er.arv_period = e.EpsRevisionsPeriod;
                    er.arv_sym_cd = sym.Code;
                    er.arv_up_30d = e.EpsRevisionsUp30Days;
                    er.arv_up_7d = e.EpsRevisionsUp7Days;

                    eh.aeh_dt = e.EarningsHistoryDate;
                    eh.aeh_eps_act = e.EarningsHistoryActuals;
                    eh.aeh_eps_diff = e.EarningsHistoryDifference;
                    eh.aeh_eps_est = e.EarningsHistoryEstimate;
                    eh.aeh_eps_surp = e.EarningsHistorySurprise;
                    eh.aeh_exc_cd = sym.Exchange.Code;
                    eh.aeh_sym_cd = sym.Code;

                    et.aet_current_est = e.EpsTrendsCurrentEstimate;
                    et.aet_curt_fg = e.EpsTrendsCurrent;
                    et.aet_dt = e.EpsTrendDate;
                    et.aet_est_30d = e.EpsTrends30DaysAgo;
                    et.aet_est_60d = e.EpsTrends60DaysAgo;
                    et.aet_est_7d = e.EpsTrends7DaysAgo;
                    et.aet_est_90d = e.EpsTrends90DaysAgo;
                    et.aet_exc_cd = sym.Exchange.Code;
                    et.aet_period = e.EpsTrendsPeriod;
                    et.aet_sym_cd = sym.Code;

                    context.RawEarningsEstimates.Add(ee); // ok
                    context.SaveChanges();
                    context.RawEarningsHistories.Add(eh); // ok
                    context.SaveChanges();
                    context.RawGrowthEstimates.Add(ge);
                    context.SaveChanges();
                    context.RawEpsRevisions.Add(er);
                    context.SaveChanges();
                    context.RawEpsTrends.Add(et);
                    context.SaveChanges();
                    context.RawRevenueEstimates.Add(re);
                    context.SaveChanges();
                }
            }
        }
    }
}
